package com.cdks.wuxiu.api.service.mysql;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdks.wuxiu.api.mapper.DailyUserInfoMapper;
import com.cdks.wuxiu.api.model.dto.UserAnswerStatisDTO;
import com.cdks.wuxiu.api.util.TimeUtils;
import com.cdks.wuxiu.entity.mysql.DailyUserInfo;

import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;


/**
 * 每日用户信息 服务实现类
 */
@Slf4j
@Service
public class DailyUserInfoServiceImpl extends ServiceImpl<DailyUserInfoMapper, DailyUserInfo> implements DailyUserInfoService {

    @Override
    public int addCallInCount(Date date, long answerId) {
        int ret  = baseMapper.addCallInCount(date, answerId);
        //如果用户登录跨天，则 daily_user_info 表当天没有记录，所以要新插入
        if (ret == 0)
        {
            DailyUserInfo info  = new DailyUserInfo();
            info.setDate(date);
            info.setUserId(answerId);
            info.setCallInCnt(1);

            baseMapper.insert(info);
        }

        return 0;
    }

    @Override
    public int addAnswerCount(Date date, long answerId) {
        baseMapper.addAnswerCount(date, answerId);

        return 0;
    }

    @Override
    public int addAnswerTime(Date date, long userId, long duration) {
        if (duration == 0)
        {
            return 0;
        }

        int ret  = baseMapper.addAnswerTime(date, userId, duration);
        if (ret == 0)
        {
            log.warn("addAnswerTime failed: userId = " + userId + ", date = " + date + ", duration = " + duration);

            return -1;
        }

        return 0;
    }

    @Override
    public int updateAnswerInfo(Date date, long userId, long duration) {
        int ret  = baseMapper.updateAnswerInfo(date, userId, duration);
        //如果用户登录跨天，则 daily_user_info 表当天没有记录，所以要新插入
        if (ret == 0)
        {
            DailyUserInfo info  = new DailyUserInfo();
            info.setDate(date);
            info.setUserId(userId);
            info.setCallInCnt(1);
            info.setAnswerCnt((duration > 0) ? 1 : 0);
            info.setAnswerTime(duration);

            baseMapper.insert(info);
        }

        return 0;
    }

    @Override
    public int setIdleTime(long userId) {
        long now  = TimeUtils.getTimeSecond();
        Date date = new Date(now * 1000);
        int ret  = baseMapper.setIdleTime(date, userId, now);
        //如果用户登录跨天，则 daily_user_info 表当天没有记录，所以要新插入
        if (ret == 0)
        {
            DailyUserInfo info  = new DailyUserInfo();
            info.setDate(date);
            info.setUserId(userId);
            info.setLastIdle(now);

            baseMapper.insert(info);
        }

        return 0;
    }

    @Override
    public UserAnswerStatisDTO getAnswerStatis(Long answerId) {
        int answerRate   = 0;
        int callAvgTime  = 0;
        int callInCnt    = 0;
        int answerCnt    = 0;
        int answerTime   = 0;


        List<DailyUserInfo> list  = baseMapper.getStatisList(answerId);
        for (DailyUserInfo info : list) {
            if (info.getAnswerCnt() == 0)
            {
                continue;
            }

            callInCnt    += info.getCallInCnt();
            answerCnt    += info.getAnswerCnt();
            answerRate   += info.getAnswerCnt() * 100 / info.getCallInCnt();
            callAvgTime  += info.getAnswerTime() / info.getAnswerCnt();
            answerTime   += info.getAnswerTime();
        }

        UserAnswerStatisDTO statis  = new UserAnswerStatisDTO();
        statis.setAnswerTime(answerTime);
        statis.setAnswerCnt(answerCnt);
        statis.setCallInCnt(callInCnt);
        statis.setAnswerRate(list.isEmpty() ? 0 : answerRate / list.size());
        statis.setCallAvgTime(callAvgTime / 7);

        return statis;
    }
}
